﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL.Entities;
using BAL;
using System.Security.Cryptography;

namespace GrouponDesktop
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtUsername.Text != string.Empty && txtPassword.Text != string.Empty)
                {
                    //Recogemos los datos del form y encriptamos el password
                    UsuarioEntity user = new UsuarioEntity();
                    user.VarPassword = GetSHA256(txtPassword.Text);
                    user.VarUsername = txtUsername.Text;

                    UsuarioBAL proxy = new UsuarioBAL();
                    int numero = proxy.LoginUser(user);

                    switch (numero)
                    {
                        case 0:
                            /*Buscamos el id de usuario*/
                            Main wind = new Main(user);
                            wind.FormClosed += new FormClosedEventHandler(Main_FormClosed);
                            wind.Show();
                            this.Hide();
                            break;
                        case 1:
                            txtRes.Text = "El usuario o le password son incorrectos.";
                            txtPassword.Text = string.Empty;
                            txtUsername.Text = string.Empty;
                            break;
                        case 2:
                            txtRes.Text = "Este usuario se encuentra bloqueado.";
                            txtPassword.Text = string.Empty;
                            txtUsername.Text = string.Empty;
                            break;
                        case 3:
                            SetPassword.SetPassword setPassword = new GrouponDesktop.SetPassword.SetPassword(txtUsername.Text);
                            setPassword.ShowDialog();
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private string GetSHA256(string text)
        {
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] hashValue;
            byte[] message = UE.GetBytes(text);

            SHA256Managed hashString = new SHA256Managed();
            string hex = "";

            hashValue = hashString.ComputeHash(message);
            foreach (byte x in hashValue)
            {
                hex += String.Format("{0:x2}", x);
            }
            return hex;
        }



        private void Main_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Close();
        }

        private void btnRegistrarUsuario_Click(object sender, EventArgs e)
        {
            RegistrarSeleccionarTipo form = new RegistrarSeleccionarTipo();
            form.ShowDialog();
        }
    }
}
